package cn.agiledata.bank.transaction.action;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.hibernateBean.AccountBank;
import cn.agiledata.bank.hibernateBean.Transaction;
import cn.agiledata.bank.transaction.exception.TransactionException;
import cn.agiledata.bank.transaction.form.DrawForm;
import cn.agiledata.bank.transaction.form.DrawToSelfForm;
import cn.agiledata.bank.transaction.service.AmountLimitService;
import com.vnv.infrastructure.logic.IPBAppFactory;

/**
 * 提现到自己银行账户Action
 * 
 * @author maxianbo
 * 
 */
public class DrawToSelfAction extends DrawAction {

    protected String getActionType() {
        return Constant.ACTION_TYPE_D; // 交易类型：提现到自己银行账户
    }

    protected String getPermitType() {
        return Constant.PERMIS_TYPE_GAINMONEY; // 权限类型：转出
    }

    protected Transaction createTransaction(DrawForm drawForm) {
        DrawToSelfForm form = (DrawToSelfForm) drawForm;
        Transaction trans = super.createTransaction(drawForm);
        String accountBankId = String.valueOf(form.getAccountBankId());
        trans.setAccountBankSeq(accountBankId);
        AccountBank accountBank = getAccountTemplate().getAccountBank(
                accountBankId);
        trans.setCardNo(accountBank.getAccountId());
        trans.setBankSeq(accountBank.getBankInfo().getId());
        trans.setBankName(accountBank.getBankName());
        trans.setBankProvSeq(accountBank.getProvSeq());
        trans.setBankCitySeq(accountBank.getCitySeq());
        trans.setPersonName(accountBank.getAccountName());
        return trans;
    }

    public ActionForward prepare(ActionMapping mapping, ActionForm actionForm,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ActionForward forward = super.prepare(mapping, actionForm, request,
                response);
        loadAccountBanks(request);
        return forward;
    }

    /**
     * 加载关联银行卡列表
     */
    private void loadAccountBanks(HttpServletRequest request) {
        long accountId = getCurrentAccount(request).getId().longValue();
        List accountBanks = getAccountService().getAccountBanksByAccountId(
                accountId);
        request.setAttribute("accountBankList", accountBanks);
    }

    /**
     * 加载收款银行账户信息
     */
    private void loadAccountBank(HttpServletRequest request, DrawToSelfForm form) {
        String accountBankId = String.valueOf(form.getAccountBankId());
        AccountBank accountBank = getAccountTemplate().getAccountBank(
                accountBankId);
        request.setAttribute("accountBank", accountBank);
    }

    public ActionForward fill(ActionMapping mapping, ActionForm actionForm,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ActionForward forward = super.fill(mapping, actionForm, request,
                response);
        DrawToSelfForm form = (DrawToSelfForm) actionForm;
        // 未进行过交易的款项不能提现到自己银行账户
        AmountLimitService als = (AmountLimitService) IPBAppFactory
                .getObj("amountLimitService");
        BigDecimal drawableAmount = als.getAmountDrawableToSelf(
                getCurrentAccount(request), new Long(form.getSubAccountId()));
        BigDecimal amount = new BigDecimal(new Double(form.getAmount())
                .doubleValue());

        if (drawableAmount.compareTo(amount) < 0) {
            throw TransactionException.NOT_TRANSACTED;
        }

        loadAccountBank(request, form);
        // 如果存在义务逻辑异常，重新装载关联银行卡列表
        if (hasAppExceptionMessage(request)) {
            loadAccountBanks(request);
        }
        return forward;
    }

    public synchronized ActionForward draw(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        ActionForward forward = super.draw(mapping, actionForm, request,
                response);
        // 如果存在义务逻辑异常，重新装载收款银行账户信息
        if (hasAppExceptionMessage(request)) {
            DrawToSelfForm form = (DrawToSelfForm) actionForm;
            loadAccountBank(request, form);
        }
        return forward;
    }

}
